import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:learn_flutter/generated/l10n.dart';
import 'package:learn_flutter/lesson10/i18n/localizations.dart';
import 'package:learn_flutter/lesson10/i18n/localizations_delegate.dart';

/// 国际化
main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.teal),
      supportedLocales: S.delegate.supportedLocales,
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        HYLocalizationsDelegate.delegate,
        S.delegate
      ],
      // locale: Locale.fromSubtags(languageCode: 'en'),
      debugShowCheckedModeBanner: false,
      home: HYHomePage(),
    );
  }
}

class HYHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(S.of(context).title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(S.of(context).hello, style: TextStyle(fontSize: 20)),
              RaisedButton(
                  child: Text(S.of(context).message, style: TextStyle(fontSize: 20)),
                  onPressed: () {
                    showDatePicker(
                      context: context,
                      initialDate: DateTime.now(),
                      firstDate: DateTime(2000),
                      lastDate: DateTime(2027),
                    );
                  }),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () => print('FloatingActionButton click')),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat);
  }
}
